﻿using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace NCommonLib.Extensions
{
    public static class HttpExtensions
    {
        public static Task<WebResponse> GetResponseAsync(this WebRequest source)
        {
            return Task<WebResponse>.Factory.FromAsync(new Func<AsyncCallback, object, IAsyncResult>(source.BeginGetResponse), new Func<IAsyncResult, WebResponse>(source.EndGetResponse), null);
        }

        public static Task<Stream> GetRequestStreamAsync(this WebRequest source)
        {
            return Task<Stream>.Factory.FromAsync(new Func<AsyncCallback, object, IAsyncResult>(source.BeginGetRequestStream), new Func<IAsyncResult, Stream>(source.EndGetRequestStream), null);
        }
    }
}
